using Microsoft.AspNetCore.Mvc;
using MallSystembackend.Api.Repository;
using MallSystembackend.Api.Model;
using Microsoft.Extensions.Configuration;
using MallSystembackend.Api.ParamModel;
using System.Linq;
namespace MallSystembackend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class ParameterValuesController : ControllerBase
    {
        public IRepository<ParameterValues> _parameterValuesRepository;
        public readonly IConfiguration _configuration;

        public ParameterValuesController(IRepository<ParameterValues> parameterValuesRepository,
        IConfiguration configuration
        )
        {
            _parameterValuesRepository = parameterValuesRepository;
            _configuration = configuration;
        }
        //添加对应的参数值
        [HttpPost]
        public ActionResult addParamParameterValues(ParamParameterValues model)
        {
            var ParameterValues = new ParameterValues
            {
                ParameterValue = model.ParameterValue,
                Sku = model.id
            };
            _parameterValuesRepository.Insert(ParameterValues);
            return Ok(new
            {
                Code = 1000,
                Data = ParameterValues,
                Msg = "添加成功"
            });
        }
        //修改对应的参数值
        [HttpPut("{id}")]
        public ActionResult UpdateParameterValues(ParamParameterValues model, int id)
        {
            var ParamParameterValues = _parameterValuesRepository.GetById(id);
            if (ParamParameterValues != null)
            {
                ParamParameterValues.ParameterValue = model.ParameterValue;
                ParamParameterValues.Sku = model.id;
            }
            _parameterValuesRepository.Update(ParamParameterValues);
            return Ok(new
            {
                Code = 1000,
                Data = ParamParameterValues,
                Msg = "修改成功"
            });
        }
        //删除对应参数值
        [HttpDelete("{id}")]
        public ActionResult DeletedParamParameterValues(int id)
        {
            var ParamParameterValues = _parameterValuesRepository.GetById(id);
            if (ParamParameterValues != null)
            {
                ParamParameterValues.IsDeleted = true;
                _parameterValuesRepository.Update(ParamParameterValues);
                return Ok(new
                {
                    Code = 1000,
                    Data = ParamParameterValues,
                    Msg = "删除成功"
                });
            }
            return Ok(new
            {
                Code = 1002,
                Data = ParamParameterValues,
                Msg = "删除失败"
            });
        }
        //根据sku获得对应值
        [HttpGet("{id}")]
        public ActionResult GetParamParameterValues(int id)
        {
            var ParamParameterValues = _parameterValuesRepository.Table;
            var ParamParameterValues1 = ParamParameterValues.Where(x => x.Sku == id)
            .Where(x => x.IsDeleted == false);
            return Ok(new
            {
                Code = 1000,
                Data = ParamParameterValues1,
                Msg = "获得成功"
            });
        }

    }
}